package com.sena.crawler.util;

import com.sena.crawler.enums.RegexTypeEnums;
import com.sena.crawler.enums.UrlTypeEnums;
import org.apache.commons.lang3.StringUtils;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * regex tool
 *
 * @author seven 2015-5-12 23:57:48
 */
public class RegexUtil {


    private static final String URL_REGEX = "http(s)://([\\w-]+\\.)+[\\w-]+(/[\\w-./?%&=]*)?$";
    private static final String CHINESE_REGEX = "[\\u4e00-\\u9fa5]+";
    private static final String NUN_REGEX = "^[-\\+]?[\\d]*$";


    /**
     * 正则匹配
     *
     * @param regex : 正则表达式
     * @param str   : 待匹配字符串
     * @return boolean
     */
    public static boolean matches(String regex, String str) {
        //正则表达式处理
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(str);
        return matcher.matches();
    }

    public static void main(String[] args) {
        System.out.println(matches("machinery.ccsu.xk.hnlat.com","http://machinery.ccsu.xk.hnlat.com/index.php?m=currency&c=fund&a=show&catid=1715&id=8470&url=http%3A%2F%2Fwww.nsfc.gov.cn%2Fpublish%2Fportal0%2Ftab442%2Finfo79133.htm"));
    }

    /**
     * url格式校验
     *
     * @param str
     * @return boolean
     */
    public static boolean isUrl(String str) {
        if (str == null || str.trim().length() == 0) {
            return false;
        }
        return matches(URL_REGEX, str);
    }

    public static String escapeExprSpecialWord(String regex) {
        if (StringUtils.isNotBlank(regex)) {
            String[] fbsArr = {"\\", "$", "(", ")", "*", "+", ".", "[", "]", "?", "^", "{", "}", "|"};
            for (String key : fbsArr) {
                if (regex.contains(key)) {
                    regex = regex.replace(key, "\\" + key);
                }
            }
        }
        return regex;
    }

    public static boolean isChinese(String str) {

        if (StringUtils.isBlank(str)) {
            return false;
        } else {
            Pattern r = Pattern.compile(CHINESE_REGEX);
            Matcher m = r.matcher(str);
            return m.find();
        }
    }

    public static boolean isInteger(String str) {
        Pattern r = Pattern.compile(NUN_REGEX);
        return r.matcher(str).matches();
    }



    public static List<String> findUrl(String str) {
        List<String> list = new LinkedList<>();
        String pattern = "(https?|ftp|file)://[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]";
        Pattern r = Pattern.compile(pattern);
        Matcher matcher = r.matcher(str);
        while (matcher.find()) {
            list.add(matcher.group());
        }
        return list;

    }


    /**
     * @param str
     * @param
     * @return
     */
    public static HashMap<String, HashMap<String, Set<String>>> regex(String str, String url) {
        HashMap<String, HashMap<String, Set<String>>> listHashMap = new HashMap<>();
        HashMap map = new HashMap();
        for (RegexTypeEnums result : RegexTypeEnums.values()) {
            Pattern r = Pattern.compile(result.getCode());
            Matcher matcher = r.matcher(str);
            Set<String> strings = new HashSet<>();
            while (matcher.find()) {
                strings.add(matcher.group());
            }
            if (strings.size() > 0) {
                map.put(result.getDesc(), strings);
            }
        }
        if (!map.isEmpty()) {
            listHashMap.put(url, map);
        }
        return listHashMap;
    }




}